﻿String.prototype.insertAt = function (index, string) {
    return this.substr(0, index) + string + this.substr(index);
};
(function ($) {
    $.fn.AddToCart = function () {
        $(this).on("click", function () {
            var ProductId = $(this).attr("ProductId");
            var Amount = $(this).attr("Amount");
            if ($("#txt-cout-product").hasClass("txt-cout-product")) {
                Amount = $("#txt-cout-product").val();
            }
            $.ajax({
                type: "GET",
                url: '/ShoppingCart/AddToCart',
                data: {
                    ProductId: ProductId,
                    Amount: Amount
                },
                traditional: true,
                dataType: 'json',
                success: function (e) {
                    $.topAlertPopup({ "text": "Thêm vào giỏ hàng thành công", "typeAlert": "success", "delay": "900" });
                    RefreshHeader();
                    if ($(".cart-view").length > 0) {
                        RefreshCart();
                    }
                }
            });
        });
    }
    function RefreshCart() {
        $.get("/ShoppingCart/_ListCart", function (data) {
            $(".cart-view").html(data);
        });
    }
    function RefreshHeader() {
        $.get("/ShoppingCart/_CartHeader", function (data) {
            $(".nav-cart-box").html(data);
        });
    }
} (jQuery));

(function ($) {
    $(document).ready(function (e) {
        $(".btn-add-cart").AddToCart();
        $("#txtNavSearch").kyersAutoComplete({ "url": "/Search/GetSearchJson" });
    });
} (jQuery));